#include "file_reader.h"

file_reader::file_reader()
{
    this->buffer=0;
    this->indicator=0;
}


void file_reader::open(QString path)
{
    this->file.open (path.toStdString().c_str(), std::ios_base::in|std::ios_base::binary );
    if(!file.is_open())
    {
        qDebug()<<"nie udalo sie otworzyc pliku";
    }
}
int file_reader::get(int length)
{
    int getter;
    if(file.is_open())
    {
        char byte;
        while(indicator<length)
        {
            file.get(byte);
            if(!file.good())
            {
                //qDebug()<<"EOF reached, break";
                return -1;
            }
            buffer=buffer<<8;
            buffer=(buffer | (byte&0xFF));
            indicator+=8;
        }
        getter=(buffer& ((((long long)1<< length)-1)<<(indicator-length)))>>(indicator-length);
        indicator-=length;
        buffer=buffer &(((long long)1<<indicator)-1);
        return getter;

    }
    else
    {
        return -1;
        qDebug()<<"Open file first";
    }
}
void file_reader::close(void)
{
    if(indicator!=0)
    {
        qDebug()<<"buffer not empty";
    }
    file.close();
}
